import request from '../utils/request'

// 获取仪表板数据
export function getDashboardData(type) {
    const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
    return request({
        url: `/api/analysis/${type}`,
        method: 'get',
        params: {
            userId: userInfo.id
        }
    })
}

// 检查仪表板访问权限
export function checkDashboardPermission(type) {
    const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
    return request({
        url: `/api/analysis/${type}/check`,
        method: 'get',
        params: {
            userId: userInfo.id
        }
    })
}

// 获取分析数据
export function getAnalysisData(type, params) {
    return request({
        url: `/api/dashboard/${type}`,
        method: 'post',
        data: params
    })
}

// 获取筛选选项
export async function getFilterOptions() {
    const [categories, cities, shops, genders, ageRanges] = await Promise.all([
        request({ url: '/api/dashboard/categories', method: 'get' }),
        request({ url: '/api/dashboard/cities', method: 'get' }),
        request({ url: '/api/dashboard/shops', method: 'get' }),
        request({ url: '/api/dashboard/genders', method: 'get' }),
        request({ url: '/api/dashboard/age-ranges', method: 'get' })
    ])

    return {
        categories: categories,
        cities: cities,
        shops: shops.map(String),  // 转换为字符串以匹配现有代码
        genders: genders,
        ageRanges: ageRanges,
        channels: ['线下'],  // 保持不变，因为后端没有提供
        weeks: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']  // 保持不变，因为后端没有提供
    }
}

// 获取核心分析数据
export function getCoreAnalysis(params) {
    return request({
        url: '/api/dashboard/core',
        method: 'post',
        data: {
            startDate: params.startDate || null,
            endDate: params.endDate || null,
            category: params.category || null,
            city: params.city || null,
            shopId: params.shopId || null,
            gender: params.gender || null,
            ageRange: params.ageRange || null
        }
    })
}

// 获取产品分析数据
export function getProductAnalysis(params) {
    return request({
        url: '/api/dashboard/product',
        method: 'post',
        data: {
            startDate: params.dateRange?.[0] || params?.startDate || null,
            endDate: params.dateRange?.[1] || params?.endDate || null,
            category: params.category || null
        }
    })
}

// 获取客户分析数据
export function getCustomerAnalysis(params) {
    return request({
        url: '/api/dashboard/customer',
        method: 'post',
        data: {
            startDate: params.dateRange?.[0] || null,
            endDate: params.dateRange?.[1] || null,
            category: params.category || null
        }
    })
}

// 获取商店分析数据
export function getShopAnalysis(params) {
    return request({
        url: '/api/dashboard/shop',
        method: 'post',
        data: {
            startDate: params.startDate,
            endDate: params.endDate,
            category: params.category || null,
            city: params.city || null,
            shopId: params.shopId || null,
            operator: params.operator || null,
            amount: params.amount || null
        }
    })
}

// 获取收入分析数据
export function getIncomeAnalysis(params) {
    return request({
        url: '/api/dashboard/income',
        method: 'post',
        data: {
            startDate: params.dateRange?.[0] || params?.startDate || null,
            endDate: params.dateRange?.[1] || params?.endDate || null,
            category: params.category || null,
            city: params.city || null
        }
    })
}

// 获取详细分析数据
export function getDetailAnalysis(params) {
    return request({
        url: '/api/analysis/all',
        method: 'post',
        data: params
    })
} 